// Berkeley Open Infrastructure for Network Computing
// http://boinc.berkeley.edu
// Copyright (C) 2005 University of California
//
// This is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation;
// either version 2.1 of the License, or (at your option) any later version.
//
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// To view the GNU Lesser General Public License visit
// http://www.gnu.org/copyleft/lesser.html
// or write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

// graphics code for upper_case

#ifdef _WIN32
#include "boinc_win.h"
#include "windows.h"
#else
#include <math.h>
#include "config.h"
#endif

#include "parse.h"
#include "gutil.h"
#include "boinc_gl.h"
#include "graphics_api.h"
#include "txf_util.h"
#include "glut.h"

#include "GLHelper.h"
#include "Logo.h"
#include "./Tabs/TabbedGUI.h"


// Global Variables - Not good but here for now
 APP_INIT_DATA uc_aid;
 TabbedGUI * tabbedGUI;
 bool bFullScreen = false;
//#include "globals.h"


// Parses the users project preferences and sets the data it contains
// Color will be used for future additions
static void parse_project_prefs(char* buf) 
{
    char cs[256];
    COLOR c;
    double hue;
    if (!buf) return;
    if (parse_str(buf, "<color_scheme>", cs, 256)) {
        if (!strcmp(cs, "Tahiti Sunset")) {
            hue = .9;
        } else if (!strcmp(cs, "Desert Sands")) {
            hue = .1;
        } else {
            hue = .5;
        }
        HLStoRGB(hue, .5, .5, c);
		/*
        color[0] = c.r;
        color[1] = c.g;
        color[2] = c.b;
        color[3] = 1;
		*/
    }
}

void app_graphics_init() 
{
    boinc_get_init_data(uc_aid);
    parse_project_prefs(uc_aid.project_preferences);

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

	tabbedGUI = new TabbedGUI();

	tabbedGUI->init(uc_aid);

}// app_graphics_init

extern "C" 
{

	void app_graphics_render(int xs, int ys, double time_of_day) 
	{
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	

		// Render the Windowed GUI
		glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		tabbedGUI->draw(xs, ys, bFullScreen);	

	}// app_graphics_render

	void app_graphics_resize(int w, int h)
	{
		int viewport[4];
		int screenWidth;
		int screenHeight;

		get_viewport(viewport);
		glViewport(0, 0, w, h);

#ifdef _WIN32
		screenWidth = GetSystemMetrics(SM_CXSCREEN);
		screenHeight = GetSystemMetrics(SM_CYSCREEN);

		if (w == screenWidth && h == screenHeight)
			bFullScreen = true;
		else
			bFullScreen = false;

#else
		
#endif

		tabbedGUI->resize(w, h, bFullScreen);

	}

	void app_graphics_reread_prefs()
	{
		boinc_parse_init_data_file();
		boinc_get_init_data(uc_aid);
		parse_project_prefs(uc_aid.project_preferences);
	}

	void app_graphics_close ()
	{
		if (tabbedGUI != NULL)
		{
			delete tabbedGUI;
			tabbedGUI = NULL;
		}// if
	}// app_graphics_close

	void boinc_app_mouse_move(int x, int y, int left, int middle, int right) 
	{
		tabbedGUI->handleMouseMove(x, y, left, middle, right);
	}

	void boinc_app_mouse_button(int x, int y, int which, int is_down) 
	{
		tabbedGUI->handleMouseButton(x, y, which, is_down);
	}// boinc_app_mouse_button

	void boinc_app_key_press(int key1, int key2)
	{
		tabbedGUI->handleKeyPress(key1, key2);
	}

	void boinc_app_key_release(int key1, int key2)
	{
		tabbedGUI->handleKeyRelease(key1, key2);
	}

}// extern "C"
